﻿@page "/admin/config"
@using System.Linq.Expressions;
@using Microsoft.AspNetCore.Authorization;
@using System.Reflection;
@using ThingsGateway.Admin.Application;
@using ThingsGateway.Core.Extension

@namespace ThingsGateway.Admin.Blazor
@attribute [Authorize]
@inject UserResoures UserResoures
@inherits BaseComponentBase
@attribute [RolePermission]




<MCard Rounded=true Elevation=1 Class="ma-2">

    <MTabs @bind-Value="tab" Class="">
        <MTab Value=1>    @AppService.I18n.T("登录策略")    </MTab>
        <MTab Value=2>    @AppService.I18n.T("密码策略")    </MTab>
        <MTab Value=3>    @AppService.I18n.T("其他")    </MTab>
    </MTabs>

    <MTabsItems Value="tab">
        <MTabItem Value="1">
            <MCard Flat Elevation="0" Class="ma-5">
                 <MRow>
                     @foreach (var item in _loginConfig)
                    {
                        switch (item.ConfigKey)
                        {
                            case ConfigConst.LOGIN_SINGLE_OPEN:
                            case ConfigConst.LOGIN_CAPTCHA_OPEN:

                                <MCol Cols="12">
                                    <MRadioGroup @bind-Value=@item.ConfigValue Label=@(item.Remark)
                                                 Style="max-width:300px" Class="ma-3"
                                                  Row Dense HideDetails="@("auto")">
                                         <MRadio Label=@AppService.I18n.T("是") Value=true.ToString()></MRadio>
                                         <MRadio Label=@AppService.I18n.T("否") Value=false.ToString()></MRadio>
                                     </MRadioGroup>

                                 </MCol>
                                break;
                            default:
                                <MCol Cols="12">

                                    <MTextField Dense Outlined HideDetails="@("auto")"
                                                 Style="max-width:300px" Class="ma-3"
                                                 Label=@(item.Remark) @bind-Value=@item.ConfigValue />
                                 </MCol>
                                break;
                        }
                    }
                </MRow>

                <MCardActions>
                    <MButton Disabled=@(!UserResoures.IsHasButtonWithRole("SysConfigLogin")) Class="ml-2 mt-5 mb-2" OnClick=loginOnSaveAsync Color="primary">
                        @AppService.I18n.T("保存")
                    </MButton>
                </MCardActions>
            </MCard>
        </MTabItem>

        <MTabItem Value="2">
            <MCard Flat Elevation="0" Class="ma-5">
                 <MRow>
                     @foreach (var item in _passwordConfig)
                    {
                        switch (item.ConfigKey)
                        {
                            case ConfigConst.PWD_CONTAIN_NUM:
                            case ConfigConst.PWD_CONTAIN_CHARACTER:
                            case ConfigConst.PWD_CONTAIN_UPPER:
                            case ConfigConst.PWD_CONTAIN_LOWER:
                                <MCol Cols="12">
                                    <MRadioGroup @bind-Value=@item.ConfigValue Label=@(item.Remark)
                                                 Style="max-width:300px" Class="ma-3"
                                                  Row Dense HideDetails="@("auto")">
                                         <MRadio Label=@AppService.I18n.T("是") Value=true.ToString()></MRadio>
                                         <MRadio Label=@AppService.I18n.T("否") Value="false.ToString()"></MRadio>
                                     </MRadioGroup>

                                 </MCol>
                                break;

                            default:
                                <MCol Cols="12">

                                    <MTextField Dense Outlined HideDetails="@("auto")"
                                                 Style="max-width:300px" Class="ma-3"
                                                 Label=@(item.Remark) @bind-Value=@item.ConfigValue />
                                 </MCol>
                                break;
                        }
                    }
                </MRow>

                <MCardActions>
                    <MButton Disabled=@(!UserResoures.IsHasButtonWithRole("SysConfigPassword")) Class="ml-2 mt-5 mb-2" OnClick=passwordOnSaveAsync Color="primary">
                        @AppService.I18n.T("保存")
                    </MButton>
                </MCardActions>
            </MCard>
        </MTabItem>
        <MTabItem Value="3" Style=@(IsMobile? "max-height:100%;":$"max-height: calc(100vh - {BlazorAppService.DefaultHeight+50}px)")>
            <MCard Flat Elevation="0" Class="ma-5">

                 <AppDataTable @ref="_datatable" TItem="SysConfig" SearchItem="ConfigPageInput"
                               AddItem="ConfigAddInput" EditItem="ConfigEditInput" SearchModel="@_search"
                               AddCallAsync="AddCallAsync" QueryCallAsync="QueryCallAsync" EditCallAsync="EditCallAsync" DeleteCallAsync="DeleteCallAsync"
                                IsShowSearchKey IsShowDetailButton IsShowQueryButton
                               IsShowAddButton=@UserResoures.IsHasButtonWithRole("SysConfigAdd")
                               IsShowDeleteButton=@UserResoures.IsHasButtonWithRole("SysConfigDelete")
                               IsShowEditButton=@UserResoures.IsHasButtonWithRole("SysConfigEdit")>
                     <AddTemplate>
                         @GetRenderFragment(context)
                    </AddTemplate>

                    <EditTemplate>
                        @GetRenderFragment(context)
                    </EditTemplate>
                </AppDataTable>

            </MCard>


        </MTabItem>



    </MTabsItems>


</MCard>


@code {
    StringNumber tab;
}




@code {

    RenderFragment GetRenderFragment(ConfigAddInput context)
    {
        RenderFragment renderFragment =
    @<MRow>

        <MCol Cols="12">

            <MTextField Dense Outlined HideDetails="@("auto")"
                        Style="max-width:300px"
                        Label=@(context.Description(x => x.ConfigKey)) @bind-Value=@context.ConfigKey />
        </MCol>
        <MCol Cols="12">

            <MTextarea Dense Outlined HideDetails="@("auto")"
                       Style="max-width:300px"
                       Label=@(context.Description(x => x.ConfigValue)) @bind-Value=@context.ConfigValue />
        </MCol>
        <MCol Cols="12">

            <MTextField Dense Outlined HideDetails="@("auto")"
                        Style="max-width:300px"
                        Label=@(context.Description(x => x.Remark)) @bind-Value=@context.Remark />
        </MCol>


        <MCol Cols="12">
            <MSlider TValue="int" ThumbLabel="@("always")" Dense
                     Style="max-width:300px;" Class="mt-8"
                     @bind-Value=@context.SortCode Label=@(context.Description(x => x.SortCode))>
                <PrependContent>
                    <MIcon OnClick="()=>context.SortCode--">
                        mdi-minus
                    </MIcon>
                </PrependContent>

                <AppendContent>
                    <MIcon OnClick="()=>context.SortCode++">
                        mdi-plus
                    </MIcon>
                </AppendContent>
            </MSlider>
        </MCol>

    </MRow>

    ;
        return renderFragment;
    }

}

